%% Author: John
%% Created: 2010-6-21
%% Description: TODO: Add description to kvs
-module(kvs).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([lookup/1, store/2, start/0]).

%%
%% API Functions
%%

%%
%% TODO: Add description of lookup/function_arity
%%
lookup(Key) -> 
	rpc({lookup,Key}).
%%
%% TODO: Add description of store/function_arity
%%
store(Key, Value) -> 
	rpc({store,Key,Value}).
%%
%% TODO: Add description of start/function_arity
%%
start() -> 
	register(kvs, spawn(fun()-> loop() end)).


%%
%% Local Functions
%%
rpc(Q)->
	kvs!{self(),Q},
	receive
		{kvs,Reply}->
			Reply
	end.

loop()->
	receive
		{From ,{store,Key,Value}}->
			put(Key,{ok,Value}),
			From!{kvs,true},
			loop();
		{From ,{lookup,Key}}->
			From!{kvs,get(Key)},
			loop()
	end.
